//
//  LSReceiveListVC.m
//  HappyStore
//
//  Created by 陈锋 on 2016/10/28.
//  Copyright © 2016年 LLJ. All rights reserved.
//

#import "LSReceiveListVC.h"
#import "LSReceiveListHeadView.h"
#import "LSReceiveListCell.h"

@interface LSReceiveListVC ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) NSArray * peopleList;
@end

static NSString * identifier = @"LSReceiveListCell";
@implementation LSReceiveListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.delegate = self;
    self.tableView.dataSource =self;
    
    LSReceiveListHeadView * LSReceiveListHead = [[LSReceiveListHeadView alloc]init];
    LSReceiveListHead = [[[NSBundle mainBundle] loadNibNamed:@"LSReceiveListHeadView" owner:self options:nil] lastObject];
    LSReceiveListHead.frame = CGRectMake(0, 0, screenW, 250);
    
    _peopleList = self.dataDict[@"data"][@"robRecord"];
    
    for (NSDictionary * dict in _peopleList) {
        NSString * str = dict[@"robUserId"];
        if ([str integerValue] == [allConstMemberid integerValue]) {
                LSReceiveListHead.takePriceLabel.text = [NSString stringWithFormat:@"我抢到%@元",dict[@"robMoney"]];
        }
    }
    
    [LSReceiveListHead.backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    
    LSReceiveListHead.fromName.text = [NSString stringWithFormat:@"来自%@",self.dataDict[@"data"][@"hairUserName"]];

    [LSReceiveListHead.headImg sd_setImageWithURL:[NSURL URLWithString:self.dataDict[@"data"][@"hairHeadPhoto"]]];
    LSReceiveListHead.headImg.layer.masksToBounds = YES;
    LSReceiveListHead.headImg.layer.cornerRadius = 25.f;
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, LSReceiveListHead.height - 31, screenW, 30)];
    view.backgroundColor = [UIColor colorWithRed:247.0/255.0 green:247.0/255.0 blue:247.0/255.0 alpha:1];
    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(5, 2, 180, 25)];
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor grayColor];
    label.font = [UIFont systemFontOfSize:12];
    
    UILabel * rightLabel = [[UILabel alloc]initWithFrame:CGRectMake(screenW - 84, 2, 80, 25)];
    rightLabel.textAlignment = NSTextAlignmentRight;
    rightLabel.backgroundColor = [UIColor clearColor];
    rightLabel.textColor = [UIColor redColor];
    rightLabel.font = [UIFont systemFontOfSize:12];
    
        if ([self.dataDict[@"msg"] isEqualToString:@"红包已经被抢完"]) {
            
            label.text = @"红包已经被抢完";
            
        }else{
            
            if (self.dataDict[@"data"][@"receiveMoney"]) {
                
                float price = [self.dataDict[@"data"][@"redMoney"] floatValue] - [self.dataDict[@"data"][@"receiveMoney"] floatValue];
                
                label.text = [NSString stringWithFormat:@"已领取%@/%@个,剩余%.2f元",self.dataDict[@"data"][@"robNum"],self.dataDict[@"data"][@"redNum"],price];
                
            }else{
            
                label.text = @"红包已经被抢完";
            
            }
 
        }
    
    rightLabel.text = [NSString stringWithFormat:@"总金额%@元",self.dataDict[@"data"][@"redMoney"]];
    [view addSubview:rightLabel];
    [view addSubview:label];
    [LSReceiveListHead addSubview:view];
    
    self.tableView.tableHeaderView = LSReceiveListHead;
    
    // 注册
    UINib * nib = [UINib nibWithNibName:@"LSReceiveListCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:identifier];
}

-(void)back{
    
    [UIView animateWithDuration:0.4 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        self.view.transform = CGAffineTransformMakeScale(0.0001f, 0.0001f);

    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }];

}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

        return self.peopleList.count;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LSReceiveListCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    
    NSDictionary * dict = self.peopleList[indexPath.row];
    
    [cell.headImg sd_setImageWithURL:[NSURL URLWithString:dict[@"robHeadPhoto"]]];
    cell.headImg.layer.masksToBounds = YES;
    cell.headImg.layer.cornerRadius = 25.f;
    
    cell.nameLabel.text = dict[@"robUserName"];
    cell.timeLabel.text = dict[@"robTime"];
    cell.priceLabel.text = [NSString stringWithFormat:@"%@元",dict[@"robMoney"]];
    
    if ([dict[@"kingluck"] isEqualToString:@"运气王"]) {
        cell.luckKing.hidden = NO;
    }
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 70;

}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 30;

}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

    return 1;

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];//取消选中状态
}

@end
